package com.ssm.server.boxes.service.impl;

import com.ssm.server.boxes.dao.BoxDao;
import com.ssm.server.boxes.dao.BoxesDao;
import com.ssm.server.boxes.dao.ClassesDao;
import com.ssm.server.boxes.dao.ClassesTypeDao;
import com.ssm.server.boxes.dao.SlideshowDao;
import com.ssm.server.boxes.model.Box;
import com.ssm.server.boxes.model.Boxes;
import com.ssm.server.boxes.model.Classes;
import com.ssm.server.boxes.model.ClassesType;
import com.ssm.server.boxes.model.Slideshow;
import com.ssm.server.boxes.service.BoxesService;
import com.ssm.server.common.JsonData;
import com.ssm.server.consts.CodeEnum;
import com.ssm.server.sysparam.dao.SysMessageDao;
import com.ssm.server.sysparam.model.SysMessage;
import com.ssm.server.util.JsonUtil;
import com.ssm.server.util.StringUtil;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyibo
 */
@Service
@Transactional
public class BoxesServiceImpl implements BoxesService {
	private static final Logger LOGGER = Logger.getLogger(Boxes.class);
	@Autowired
	private BoxesDao boxesDao;
	@Autowired
	private BoxDao boxDao;
	@Autowired
	private ClassesDao classesDao;

	@Autowired
	private SlideshowDao slideshowDao;
	@Autowired
	private SysMessageDao sysMessageDao;
	@Autowired
	private ClassesTypeDao classesTypeDao;

	
	
	/*
	 * 根据箱号获取
	 */
	// @Override
	// public JsonData getBoxesByBoxesNumber(Map<String, String> param) throws
	// Exception {
	// Map<String, Object> maps =
	// JsonUtil.JsonToMap(param.get("json").toString());
	//
	// JsonData jsonData = new JsonData();
	// String endNumber = maps.get("endNumber").toString();
	// String startNumber = maps.get("startNumber").toString();
	// String classesId = maps.get("classesId").toString();
	//
	// try {
	// List<Boxes> boxes =
	// boxesDao.getBoxesByBoxesNumber(Integer.valueOf(startNumber),
	// Integer.valueOf(endNumber),
	// Integer.valueOf(classesId));
	//
	// jsonData.setData(boxes);
	// jsonData.setCodeEnum(CodeEnum.SUCCESS);
	// } catch (Exception e) {
	// LOGGER.error("ERROR", e);
	// jsonData.setCodeEnum(CodeEnum.ERROR);
	// }
	// return jsonData;
	// }

	@Override
	public JsonData getboxByBoxesId(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());

		JsonData jsonData = new JsonData();
		String boxesId = maps.get("boxesId").toString();
		try {
			List<Box> box = boxDao.getboxByBoxesId(Integer.valueOf(boxesId));
			jsonData.setData(box);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	@Override
	public JsonData getboxByBoxId(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());

		JsonData jsonData = new JsonData();
		String boxId = maps.get("boxId").toString();
		try {
			Box box = boxDao.getboxByBoxId(Integer.valueOf(boxId));
			jsonData.setData(box);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	/*
	 * 根据级别查询
	 */

	@Override
	public JsonData getboxByBoxLevel(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());

		JsonData jsonData = new JsonData();
		String startNumber = maps.get("startNumber").toString();
		String endNumber = maps.get("endNumber").toString();
		String boxLevel = maps.get("boxLevel").toString();
		String classesId = maps.get("classesId").toString();
		String orderbyNumberDesc = maps.get("orderbyNumberDesc").toString();
		String orderbyNowSumDesc = maps.get("orderbyNowSumDesc").toString();

		int startNumbers = 0;
		int endNumbers = 0;
		if (!StringUtil.isEmpty(startNumber) || !StringUtil.isEmpty(endNumber)) {
			startNumbers = Integer.valueOf(startNumber);
			endNumbers = Integer.valueOf(endNumber);
		}

		try {
			int boxLevels = 0;
			if (!StringUtil.isEmpty(boxLevel)) {
				boxLevels = Integer.valueOf(boxLevel);
			}

			int orderbyNumberDescs = 0;
			if (!StringUtil.isEmpty(orderbyNumberDesc)) {
				orderbyNumberDescs = 1;
			}
			int orderbyNowSumDescs = 0;
			if (!StringUtil.isEmpty(orderbyNowSumDesc)) {
				orderbyNowSumDescs = 1;
			}
			List<Boxes> boxes = boxesDao.getBoxesByBoxesNumber(Integer.valueOf(startNumbers),
					Integer.valueOf(endNumbers), Integer.valueOf(classesId), orderbyNumberDescs, orderbyNowSumDescs,
					boxLevels);
			List<Boxes> boxesList = new ArrayList<Boxes>();

			for (int i = 0; i < boxes.size(); i++) {

				int boxesId = boxes.get(i).getBoxesId();
				int boxesSumboxNow = boxDao.boxesSumboxNow(boxesId);
				int boxesSumbox = boxDao.boxesSumbox(boxesId);
				boxes.get(i).setBoxesSumboxNow(boxesSumboxNow);
				boxes.get(i).setBoxesSumbox(boxesSumbox);
				List<Box> box = boxDao.getboxByBoxesId(boxesId);
				// Box box
				// =boxDao.getboxByBoxLevel(Integer.valueOf(boxLevel),boxesId);
				if (box.size() > 0) {
					boolean isTrue = false;
					for (int j = 0; j < box.size(); j++) {
						if (box.get(j).getBoxLevel().equals(boxLevels) && box.get(j).getBoxNowSum() == 0) {
							continue;
						} else {
							if (box.get(j).getBoxLevel().equals(boxLevel)) {
								box.get(j).setIsQuery(1);
							} else {
								box.get(j).setIsQuery(0);
							}
							boxes.get(i).setBox(box);
						}
						System.out.println("++++++++++++++" + boxesId);
						if (StringUtil.isEmpty(boxLevel) || box.get(j).getBoxLevel().equals(boxLevel)) {
							isTrue = true;
						}
					}
					if (isTrue) {
						boxesList.add(boxes.get(i));
					}
				} else {
					if (StringUtil.isEmpty(boxLevel)) {
						boxes.get(i).setState(1);
						boxesList.add(boxes.get(i));
					}
				}
			}
			jsonData.setData(boxesList);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	@Override
	public JsonData getClassesByState(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());

		JsonData jsonData = new JsonData();
		String typeId = maps.get("typeId").toString();// 类别id
		int page = Integer.valueOf(param.get("page"));
		int limit = Integer.valueOf(param.get("limit"));
		try {

			int countSize = limit;
			int pageCount = (page - 1) * countSize;
			List<Classes> classes = classesDao.getClassesByState(countSize, pageCount, Integer.valueOf(typeId));
			for (int i = 0; i < classes.size(); i++) {
				int boxesCount = boxesDao.countBoxes(Integer.valueOf(classes.get(i).getClassesId()));
				classes.get(i).setNowsumBoxes(boxesCount);
			}

			jsonData.setData(classes);

			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

//	/*
//	 * 修改状态
//	 */
//	@Override
//	public JsonData updateState(Map<String, String> param) throws Exception {
//		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());
//
//		JsonData jsonData = new JsonData();
//		String state = maps.get("state").toString();
//		String classesId = maps.get("classesId").toString();
//
//		try {
//			Classes classes = new Classes();
//			classes.setClassesId(Integer.valueOf(classesId));
//			classes.setState(Integer.valueOf(state));
//			classesDao.updateState(classes);
//			jsonData.setCodeEnum(CodeEnum.SUCCESS);
//		} catch (Exception e) {
//			LOGGER.error("ERROR", e);
//			jsonData.setCodeEnum(CodeEnum.ERROR);
//		}
//		return jsonData;
//	}

	@Override
	public JsonData countBoxes(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());

		JsonData jsonData = new JsonData();
		String classesId = maps.get("classesId").toString();

		try {
			int boxes = boxesDao.countBoxes(Integer.valueOf(classesId));
			jsonData.setData(boxes);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	@Override
	public JsonData getBoxesByClassId(Map<String, String> param) throws Exception {
		Map<String, Object> maps = JsonUtil.JsonToMap(param.get("json").toString());
		JsonData jsonData = new JsonData();
		String classesId = maps.get("classesId").toString();
		String orderbyNumberDesc = maps.get("orderbyNumberDesc").toString();
		String orderbyNowSumDesc = maps.get("orderbyNowSumDesc").toString();

		int page = Integer.valueOf(param.get("page"));
		int limit = Integer.valueOf(param.get("limit"));
		try {
			int countSize = limit;
			int pageCount = (page - 1) * countSize;
			int orderbyNumberAscs = 0;
			if (!StringUtil.isEmpty(orderbyNumberDesc)) {
				orderbyNumberAscs = 1;
			}
			int orderbyNowSumAscs = 0;
			if (!StringUtil.isEmpty(orderbyNowSumDesc)) {
				orderbyNowSumAscs = 1;
			}
			List<Boxes> boxes = boxesDao.getBoxesByClassId(Integer.valueOf(classesId), orderbyNumberAscs,
					orderbyNowSumAscs, pageCount, countSize);
			for (int i = 0; i < boxes.size(); i++) {
				int boxesId = boxes.get(i).getBoxesId();
				int boxesSumboxNow = boxDao.boxesSumboxNowAll(boxesId);
				int boxesSumbox = boxDao.boxesSumbox(boxesId);

				boxes.get(i).setBoxesSumboxNow(boxesSumboxNow);
				boxes.get(i).setBoxesSumbox(boxesSumbox);
				List<Box> box = boxDao.getboxByBoxesId(boxesId);

				for (int j = 0; j < box.size(); j++) {
					if (box.get(j).getBoxNowSum() == 0 || boxesSumboxNow == 0) {
						box.get(j).setRatio(new BigDecimal(0.00));
					} else {
						BigDecimal ratio = new BigDecimal(box.get(j).getBoxNowSum())
								.divide(new BigDecimal(boxesSumboxNow), 3, BigDecimal.ROUND_HALF_UP);
						box.get(j).setRatio(ratio);
					}
				}
				boxes.get(i).setBox(box);

			}
			jsonData.setData(boxes);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	// @Override
	// public JsonData getClasses(Map<String, String> param) throws Exception {
	// JsonData jsonData = new JsonData();
	// try {
	// Map<String, Object> maps =
	// JsonUtil.JsonToMap(param.get("json").toString());
	// String state = maps.get("state").toString();
	// List<Classes> classes=
	// classesDao.getClassesByState(Integer.valueOf(state));
	// jsonData.setData(classes);
	// jsonData.setCodeEnum(CodeEnum.SUCCESS);
	// } catch (Exception e) {
	// LOGGER.error("ERROR", e);
	// jsonData.setCodeEnum(CodeEnum.ERROR);
	// }
	// return jsonData;
	// }

	@Override
	public JsonData getSlideshow(Map<String, String> param) throws Exception {
		JsonData jsonData = new JsonData();
		try {
			List<Slideshow> slideshow = slideshowDao.getSlideshow();
			for (int i = 0; i < slideshow.size(); i++) {

				Classes classes = classesDao.getClassesById(slideshow.get(i).getClassesId());

				if (classes != null) {
					int boxesCount = boxesDao.countBoxes(Integer.valueOf(classes.getClassesId()));
					classes.setNowsumBoxes(boxesCount);
					slideshow.get(i).setClasses(classes);
				}
			}
			jsonData.setData(slideshow);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}

	@Override
	public JsonData getClassesType(Map<String, String> param) throws Exception {
		JsonData jsonData = new JsonData();
		try {
			List<ClassesType> classesType = classesTypeDao.getClassesType();
			jsonData.setData(classesType);
			jsonData.setCodeEnum(CodeEnum.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("ERROR", e);
			jsonData.setCodeEnum(CodeEnum.ERROR);
		}
		return jsonData;
	}
	

}